// 登录页
<template>
  <div class="box">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="name">
        <el-input v-model="ruleForm.name"></el-input> </el-form-item
      ><el-form-item label="密码" prop="pwd">
        <el-input v-model="ruleForm.pwd"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { loginApi } from '../utile/https'
import { setCookie } from "@/utile/cookie";
export default {
  data () {
    return {
      ruleForm: {
        name: '',
        pwd: ''

      },
      rules: {
        name: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 6, message: '长度在6个字符', trigger: 'blur' }
        ],

      }
    };
  },
  created () {
    // console.log(loginApi());
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 验证通过
          loginApi(this.ruleForm).then((res) => {
            // console.log(111, res);
            if (res.code == 200) {
              setCookie('token', res.data.token)
              localStorage.setItem('role', res.data.role)
              this.$router.push('/')
              this.$message({
                type: "success",
                message: res.msg,
                duration: 1000,
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg,
                duration: 1000,
              });
            }
          })
        } else {
          console.log('登录失败');
          return false;
        }
      });
    },
    // 重置功能
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.box {
  padding-top: 20%;
  box-sizing: border-box;
  height: 100%;
  width: 100%;
  background: url(https://tse1-mm.cn.bing.net/th/id/OIP-C.3XJ54F6XlKz8Bi4pDimiFgHaE8?pid=ImgDet&rs=1)
    no-repeat center;
  background-size: cover;
}
.demo-ruleForm {
  min-width: 20%;
  height: 100%;
  max-width: 300px;
  margin: 0 auto;
  /* margin-top: 15%; */
}
</style>>

